<?php
/*
 * @Author: your name
 * @Date: 2019-10-31 17:52:01
 * @LastEditTime : 2020-01-06 16:42:17
 * @LastEditors  : Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /ahsmp_3/application/common/service/HostGuisePort.php
 */

namespace app\common\service;


class HostGuisePort extends Base 

{
    /**
     * @name: 添加
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 10:37:51
     */
    function add ($data)
    {
        db('HostGuisePort')->insertAll($data);
    }

    /**
     * @name: 删除
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 10:38:30
     */
    function delete ($host_id)
    {
        db('HostGuisePort')->whereIn('host_id', $host_id)->delete();
    }

    /**
     * @name: 详情
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 10:46:15
     */
    function info ($host_id)
    {
        $return = db('HostGuisePort')->where('host_id', $host_id)->select();

        return $return;
    }

    /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 11:26:13
     */
    function list ($filterData, $limit)
    {
        $server = db('HostGuisePort')->field('id,port,protocols,status,type');
        $server->where('host_id', $filterData['host_id']);

        isset($filterData['port']) && is_numeric($filterData['port']) ? $server->whereLike('port', '%' . $filterData['port'] . '%') : '';
        isset($filterData['protocols']) && is_numeric($filterData['protocols']) ? $server->where('protocols', $filterData['protocols']) : '';
        isset($filterData['status']) && is_numeric($filterData['status']) ? $server->where('status', $filterData['status']) : '';

        $return = $server->paginate($limit)->toArray();

        return $return;
    }

    /**
     * @name: 添加默认端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-07 15:26:40
     */
    function addDefaultPort ($host_id, $os)
    {
        if(db('HostGuisePort')->where('host_id', $host_id)->count() > 0)
        {
            return [];
        }
        
        $port = model('HostGuisePort')->defaultConfig($os);
        $insert = [];
        foreach ($port as $k => $v)
        {
            $tmp = [
                'host_id' => $host_id,
                'port' => $v,
                'protocols' => model('HostPort')->PROTOCOL_TCP,
                'status' => model('HostGuisePort')->STATUS_ZERO,
                'type' => $k,
            ];
            $insert[] = $tmp;
        }
        
        db('HostGuisePort')->insertAll($insert);
    }

    /**
     * @name: 根据主机id查询
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-06 13:16:07
     */
    function selectByHostId ($host_id_a)
    {
        $return = db('HostGuisePort')->whereIn('host_id', $host_id_a)->select();

        return $return;
    }
}